<template>
  <div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column
          :key="index"
          v-for="(item, index) in datalist"
          :prop="item.prop"
          :label="item.label"
          :sortable="'custom'"
          :align="item.align"
        >
      </el-table-column>
      <el-table-column label="操作" align="center">
        <!-- slot-scope="scope" 这里取到当前单元格 -->
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="deleteClick(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.currentPage"
      :page-sizes="[5, 10, 30, 40]"
      :page-size="page.sizeNum"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
    >
    </el-pagination>
  </div>
</template>

<script>
const datalist = [
  { label: "姓名", prop: "name", align: "center", sort: "custom" },
  { label: "学号", prop: "studentNumber", align: "center", sort: "custom" },
  { label: "年级", prop: "grade", align: "center", sort: "custom" },
  { label: "专业", prop: "major", align: "center", sort: "custom" },
  { label: "班级", prop: "clazz", align: "center", sort: "custom" },
  { label: "手机号", prop: "phone", align: "center", sort: "custom" },
  { label: "邮箱", prop: "email", align: "center", sort: "custom" },
];
export default {
  data() {
    return {
      datalist,
      tableData: [],
      page: {
        currentPage: 1, // 当前页
        sizeNum: 5, // 每页条数
      },
      totalCount: null,
    };
  },
  mounted() {
    this.getAxios();
  },
  methods: {
    handleSizeChange(val) {
      this.page.sizeNum = val;
      this.getAxios();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.getAxios();
      // console.log(`当前页: ${val}`);
    },
    deleteClick(row) {
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios.put("user/admin/delete/" + row.studentNumber).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            // console.log(res);
            this.getAxios();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    getAxios() {
      this.$axios
        .get("/user/admin/list", {
          params: {
            page: this.page.currentPage,
            limit: this.page.sizeNum,
            orderField: this.orderField,
            order: this.order,
          },
        })
        .then((res) => {
          console.log(res);
          this.totalCount = res.data.data.page.totalCount;
          this.$emit("transmit", this.totalCount);
          const data = res.data.data.page.list;
          data.forEach((item) => {
            if (item.roleId == 1) {
              item.roleId = "超级管理员";
            } else if (item.roleId == 2) {
              item.roleId = "管理员";
            }
          });
          this.tableData.length = 0;
          this.tableData = data;
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-table {
  margin-top: 10px;
  /deep/ th {
    height: 20px;
    padding: 0;
  }
  /deep/ td {
    height: 5rem;
    padding: 0;
  }
}
</style>